package cn.com.aiouyi.service.impl;

import cn.com.aiouyi.entity.Article;
import cn.com.aiouyi.entity.File;
import cn.com.aiouyi.service.AccessoryService;
import cn.com.aiouyi.service.FileService;
import cn.com.aiouyi.util.AssertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by tommy on 2018/4/26.
 */
@Service(value = "fileServiceImpl")
public class FileServiceImpl extends BaseServiceImpl<File, Long> implements FileService {

    @Resource(name = "accessoryServiceImpl")
    private AccessoryService accessoryService;

    @Override
    public void delete(Long[] ids){
        AssertUtils.notNull(ids);
        AssertUtils.isTrue(ids.length > 0);
        for (int i = 0; i < ids.length; i++) {
            File file = this.find(ids[i]);
            if (file != null){
                Long acId = file.getAcc().getId();
                this.realDelete(file);
                accessoryService.delete(acId);
            }
        }
    }

    @Override
    public void updateDownloadCount(File fileObj) {
        logger.info("下载记录：{} 加1", fileObj.getDownloadCount());
        Integer count = fileObj.getDownloadCount()==null?0:fileObj.getDownloadCount();
        fileObj.setDownloadCount(count+1);
        this.update(fileObj);
    }
}
